package com.ctcemti.mdm.project.common.excelverifyhandler;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.ctcemti.mdm.project.sys.dao.JobMapper;
import com.ctcemti.mdm.project.sys.entity.InfDictItem;
import com.ctcemti.mdm.project.sys.entity.OrgJob;

import java.util.ArrayList;
import java.util.List;

public class ExcelVerifyHandlerForOrgJob  implements IExcelVerifyHandler<OrgJob> {

    private List<String> listCode;

    private JobMapper jobMapper;

    private String loginId;

    private List<InfDictItem> list = new ArrayList<InfDictItem>();

    private List<String> temp = new ArrayList<String>();

    @Override
    public ExcelVerifyHandlerResult verifyHandler(OrgJob orgJob) {
        orgJob.setCreator(loginId);
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult();
        OrgJob entity = new OrgJob();
        entity.setCode(orgJob.getCode());
        List<OrgJob> select = jobMapper.select(entity);

        if(select!=null && select.size()>0){
            if(listCode.contains(orgJob.getCode())){
                result.setMsg("该岗位编码数据库中已存在");
                result.setSuccess(false);
                return result;
            }
        }

        if(listCode!=null && listCode.size()>0){
            if(listCode.contains(orgJob.getCode())){
                result.setMsg("该岗位编码表格中已存在");
                result.setSuccess(false);
                return result;
            }
        }
        if(!temp.contains(orgJob.getJob())){
            result.setMsg("该岗位不存在,请联系管理员添加");
            result.setSuccess(false);
            return result;
        }
        for (InfDictItem item:list){
            if(item.getName().equals(orgJob.getJob())){
                orgJob.setJob(item.getCode());
            }
        }
        listCode.add(orgJob.getCode());
        result.setSuccess(true);
        return result;
    }

    public ExcelVerifyHandlerForOrgJob(Object object,String loginId, List<InfDictItem> list){
        this.jobMapper = (JobMapper) object;
        this.loginId = loginId;
        this.list = list;
        for (InfDictItem item:list){
            temp.add(item.getName());
        }

    }
}

